<template>
  <div>
    <div class="title" style="text-align:left">
      <font-awesome-icon icon="address-book" />&nbsp;用户列表
    </div>
    <div class="block-line">&nbsp;</div>
    <el-table :data="userData" border style="width: 100%">
      <el-table-column prop="id" label="用户ID" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="regdate" label="注册日期"></el-table-column>
      <el-table-column prop="lastdate" label="最后一次登录日期"></el-table-column>
      <el-table-column prop="lock" label="用户状态"
        :filters="[{ text: '活跃', value: 0 }, { text: '锁定', value: 1 }]"
        :filter-method="filterLockTag" filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag :type="scope.row.lock === 0 ? 'success' : 'danger'"
            disable-transitions>{{showLockText(scope.row.lock)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="admin" label="用户身份"
        :filters="[{ text: '普通用户', value: 0 }, { text: '管理员', value: 1 }]"
        :filter-method="filterAdminTag" filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag :type="scope.row.admin === 0 ? 'warning' : 'danger'"
            disable-transitions>{{showAdminText(scope.row.admin)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button v-if="scope.row.lock == 0" size="mini" type="primary" @click="handleLock(scope.row)">
           <font-awesome-icon icon="lock" />&nbsp;锁定</el-button>
          <el-button v-if="scope.row.lock == 1" size="mini" type="warning" @click="handleLock(scope.row)">
           <font-awesome-icon icon="lock" />&nbsp;解锁</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
           <i class="el-icon-delete"></i>&nbsp;删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block-line">&nbsp;</div>
    <div class="text-right"><el-pagination background layout="prev, pager, next" :total="totalPage"
      :current-page.sync="currentPage" @current-change="loadUserData"></el-pagination></div>
  </div>
</template>
<script>
import { Loading } from 'element-ui'
export default {
  data () {
    return {
      currentPage: 1, // 当前所在页
      totalPage: 200, // 总共的页数
      userData: [] // 用户的列表数据
    }
  },
  methods: {
    handleLock (item) { // 【开发】用户锁定状态的修改
      if (item.lock === 0) {
        item.lock = 1 // 修改锁定状态
        this.$message.error('用户已经被锁定，暂时无法登录！')
      } else {
        item.lock = 0 // 解除锁定状态
        this.$message({type: 'success', message: '用户已经解锁，可以正常登录访问！'})
      }
    },
    handleDelete (item) { // 【开发】用户删除操作，进行ajax异步调用
      this.$confirm('确定要删除该用户信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.userData.splice(item, 1)
        this.$message({
          type: 'success',
          message: '用户删除成功!'
        })
      }).catch(() => {})
    },
    filterLockTag (value, row) {
      return row.lock === value
    },
    filterAdminTag (value, row) {
      return row.admin === value
    },
    showLockText (val) {
      if (val === 0) {
        return '活跃'
      } else {
        return '锁定'
      }
    },
    showAdminText (val) {
      if (val === 0) {
        return '普通用户'
      } else {
        return '管理员'
      }
    },
    loadUserData () { // 【开发】分页数据加载操作
      let loadingInstance = Loading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
        setTimeout(() => { // 【开发】加载用户数据信息
          this.$back.user.loadUser(this.currentPage).then(res => { // 加载新闻数据信息
            this.userData = res.data // 保存角色列表数据
          })
          loadingInstance.close()
        }, 200)
      })
    }
  },
  mounted () {
    this.loadUserData() // 加载用户数据信息
  }
}
</script>
